package cn.breeze.order.service.impl;

import cn.breeze.basic.exception.BusinessException;
import cn.breeze.order.domain.OrderPetAcquisition;
import cn.breeze.order.dto.ConfirmOrderDto;
import cn.breeze.order.mapper.OrderPetAcquisitionMapper;
import cn.breeze.order.service.IOrderPetAcquisitionService;
import cn.breeze.basic.service.impl.BaseServiceImpl;
import cn.breeze.pet.domain.Pet;
import cn.breeze.pet.domain.SearchMasterMsg;
import cn.breeze.pet.mapper.PetMapper;
import cn.breeze.pet.mapper.SearchMasterMsgMapper;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author breeze
 * @since 2022-07-20
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    /**
    * @Title: cancelOrder
    * @Description: 取消订单
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/20 18:05
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void cancelOrder(Long id) {
        // 根据id查询
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        if (orderPetAcquisition == null){
            throw new BusinessException("非法参数！");
        }
        if (orderPetAcquisition.getState() == 1 || orderPetAcquisition.getState() == -1){
            throw new BusinessException("订单已取消，请不要重复修改！");
        }
        // 将订单状态设置为-1
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 将寻主消息的shopId置空
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setShopId(null);
        searchMasterMsg.setState(1);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 删除宠物信息
        petMapper.remove(orderPetAcquisition.getPetId());
    }

    /**
    * @Title:
    * @Description: 确认订单
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/20 18:23
    * @Parameters:
    * @Return
    */
    @Override
    public void confirm(ConfirmOrderDto confirmOrderDto) {
        Long id = confirmOrderDto.getId();
        BigDecimal money = confirmOrderDto.getMoney();
        Integer payType = confirmOrderDto.getPayType();
        if (id == null || money == null || payType == null){
            throw new BusinessException("参数不能为空！");
        }
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        if (orderPetAcquisition == null){
            throw new BusinessException("订单不存在！");
        }
        if (orderPetAcquisition.getState() == 1 || orderPetAcquisition.getState() == -1){
            throw new BusinessException("订单已完成，请不要重复修改！");
        }
        // 将订单的改为1，同时修改价格
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 修改寻主消息的的状态为2
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 修改完善宠物信息
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        if (pet == null){
            throw new BusinessException("宠物信息不存在！");
        }
        pet.setCostprice(money);
        petMapper.update(pet);

    }
}
